use crate::dispatch;
use crate::parse_conf;
use crate::run_cmd::run_cmd_exe;
use crate::run_cmd::run_cmd_install;
use crate::run_cmd::run_cmd_unpack;
use crate::str_utils::get_file_name;
use download_rs::sync_download::Download;
use std::thread::sleep;
use std::time::Duration;
use std::fs;
// error_chain! {
//      foreign_links {
//          Io(std::io::Error);
//          HttpRequest(reqwest::Error);
//      }
// }

// #[tokio::main]
// #[allow(unused)]
// pub async fn down(url: String) -> Result<()> {
//     let target = url;
//     let response = reqwest::get(target).await?;

//     let mut dest = {
//         let fname = response
//             .url()
//             .path_segments()
//             .and_then(|segments| segments.last())
//             .and_then(|name| if name.is_empty() { None } else { Some(name) })
//             .unwrap_or("tmp.bin");

//         println!("file to download: '{}'", fname);
//         let mut path = std::path::PathBuf::with_capacity(1);
//         path.push(String::from("D://hello/"));
//         let fname = path.join(fname);
//         println!("will be located under: '{:?}'", fname);
//         File::create(fname)?
//     };
//     let content = response.text().await?;
//     copy(&mut content.as_bytes(), &mut dest)?;
//     Ok(())
// }

// #[allow(unused)]
// #[tokio::main]
// pub async fn download(url: String) -> Result<()> {
//     let mut this_url = url;
//     let client = reqwest::Client::new();
//     let result_body = client.get(&this_url).send().await?;
//     let mut file_name = get_file_name(this_url);
//     println!("文件 {} 正在下载！", file_name);
//     let rev = result_body.bytes().await?;
//     // 定义保存文件的位置
//     let mut file_name_path = parse_conf::parse_base_save_path() + &file_name;
//     let path = Path::new(&file_name_path);
//     fs::create_dir_all(parse_conf::parse_base_save_path()).expect("Base path 创建失败！");
//     {
//         let mut file = File::create(path).expect("文件创建失败！");
//         file.write_all(&rev)?;
//         println!(" 下载完成！");
//     }
//     sleep(Duration::from_millis(400));
//     let flag = dispatch::unpack_or_install(&file_name);
//     if flag == 0 {
//         run_cmd_unpack(parse_conf::parse_install_path(), file_name_path, file_name);
//     } else if flag == 1 {
//         run_cmd_install(file_name_path);
//     }else if flag == 2{
//         run_cmd_exe(file_name_path);
//     }
//     else {
//         println!("无法自动安装此类型文件！");
//     }
//     Ok(())
// }

pub fn download_with3(url: String) {
    let url = url.clone();
    let file_name = get_file_name(url.to_string());

    // 当前目录
    let file_name_path = parse_conf::parse_base_save_path() + &file_name;
    // 指定 下载目录
    // let filename = "/download/";
    // 指定下载目录下载文件名,需要手动创建下载文件夹
    // let filename = "download/bd_log1.png";
    fs::create_dir_all(parse_conf::parse_base_save_path()).expect("Base path 创建失败！");
    println!("文件 {} 正在下载！", file_name);
    let download = Download::new(&url, Some(&file_name_path), None);

    match download.download() {
        Ok(_) => println!("下载完成"),
        Err(e) => println!("下载出错 ： {}", e.to_string()),
    }
    sleep(Duration::from_millis(400));
    let flag = dispatch::unpack_or_install(&file_name);
    if flag == 0 {
        run_cmd_unpack(parse_conf::parse_install_path(), file_name_path, file_name);
    } else if flag == 1 {
        run_cmd_install(file_name_path);
    } else if flag == 2 {
        run_cmd_exe(file_name_path);
    } else {
        println!("无法自动安装此类型文件！");
    }
}
